package com.example.ble.ble_upgrade.util;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.example.ble.ble_upgrade.Ble_Activity;
import com.example.ble.ble_upgrade.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ljy on 2017/1/19.
 */

public class ChooseFileActivity extends Activity {
    static final public String sRoot = Environment.getExternalStorageDirectory().getAbsolutePath() ;
    static final public String sParent = "..";
    static final public String sFolder = ".";
    static final public String sEmpty = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Map<String, Integer> images = new HashMap<String, Integer>();
        images.put(sRoot, R.drawable.filedialog_root);    // 根目录图标
        images.put(sParent, R.drawable.filedialog_folder_up);    //返回上一层的图标
        images.put(sFolder, R.drawable.filedialog_folder);    //文件夹图标
        images.put("wav", R.drawable.filedialog_wavfile);    //wav文件图标
        images.put(sEmpty, R.drawable.filedialog_root);

        LinearLayout layout = new LinearLayout(this);
        LayoutInflater flater = LayoutInflater.from(this);
        View view = flater.inflate(R.layout.action_bar_title, null);
        TextView textTitle = (TextView) view.findViewById(R.id.title);
        ImageButton back = (ImageButton) view.findViewById(R.id.title_left_btn);
        textTitle.setText(getString(R.string.file));
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChooseFileActivity.this.finish();
            }
        });
        FileSelectListView fileSelectListView = new FileSelectListView(this, new CallbackBundle() {
            @Override
            public void callback(Bundle bundle) {
                String filePath = bundle.getString("path");
                Intent intent = new Intent(ChooseFileActivity.this, Ble_Activity.class);
                intent.putExtra("path", filePath);
                setResult(RESULT_OK, intent);
                finish();
            }
        }, null, images);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(view);
        layout.addView(fileSelectListView);
        layout.setBackgroundColor(Color.parseColor("#f2f2f2"));
        LinearLayout.LayoutParams Params = (LinearLayout.LayoutParams) view.getLayoutParams();
        Params.height = (int) getResources().getDimension(R.dimen.width50);
        view.setLayoutParams(Params);
        setContentView(layout);
    }
}
